package com.syq.service.impl;

import com.syq.constants.ResultCode;
import com.syq.dto.UserDto;
import com.syq.dao.SysUserMapper;
import com.syq.event.LoginEvent;
import com.syq.exceptions.AddException;
import com.syq.exceptions.QueryException;
import com.syq.pojo.CheckType;
import com.syq.pojo.SysUserInfo;
import com.syq.service.SysUserService;
import com.syq.utils.MyStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author Sun Yueqi
 * @version 1.00
 * @time 2022/5/25 20:37
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    private ApplicationContext context;

    private SysUserMapper sysUserMapper;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public void addUser(UserDto userDto) {
        Assert.isTrue(!userDto.isEmpty(CheckType.ADD),()->{
            throw new AddException("数据不完整", ResultCode.DATA_NULL);
        });

        SysUserInfo userInfo = sysUserMapper.findByName(userDto.getUsername());
        Assert.isNull(userInfo,()->{
            throw new AddException("用户已存在",ResultCode.DATA_ALREADY_EXIST);
        });
         userInfo = sysUserMapper.findByEmail(userDto.getEmail());
        Assert.isNull(userInfo,()->{
            throw new AddException("邮箱已存在",ResultCode.DATA_ALREADY_EXIST);
        });
         userInfo = sysUserMapper.findByPhone(userDto.getPhone());
        Assert.isNull(userInfo,()->{
            throw new AddException("电话已存在",ResultCode.DATA_ALREADY_EXIST);
        });

        SysUserInfo sysUserInfo = new SysUserInfo();

        BeanUtils.copyProperties(userDto,sysUserInfo);

        sysUserInfo.setCreateTime(new Date());
        sysUserInfo.setStatus(1L);
        sysUserInfo.setType(1L);
        sysUserInfo.setRemark("普通用户");
        sysUserInfo.setCreateBy(userDto.getUsername());

        String salt= MyStringUtils.creatRandomString(10);
        sysUserInfo.setPwdSalt(salt);

        String passwordMd5= DigestUtils.md5DigestAsHex((sysUserInfo.getPassword()+salt).getBytes(StandardCharsets.UTF_8));
        sysUserInfo.setPassword(passwordMd5);

        sysUserMapper.addUser(sysUserInfo);
    }

    @Override
    public SysUserInfo login(UserDto userDto) {
        Assert.isTrue(StringUtils.hasText(userDto.getUsername())&&StringUtils.hasText(userDto.getPassword()),()->{
            throw new QueryException("用户名密码不能为空",ResultCode.USERNAME_PASSWORD_NULL);
        });

        SysUserInfo userInfo = sysUserMapper.findByName(userDto.getUsername());
        Assert.notNull(userInfo,()->{
            context.publishEvent(new LoginEvent(LoginEvent.LoginType.FAIL, userDto.getUsername()));
            throw new QueryException("用户名或密码错误",ResultCode.USERNAME_PASSWORD_ERROR);
        });
        String s = DigestUtils.md5DigestAsHex((userDto.getPassword() + userInfo.getPwdSalt()).getBytes(StandardCharsets.UTF_8));
        Assert.isTrue(s.equalsIgnoreCase(userInfo.getPassword()),()->{
            context.publishEvent(new LoginEvent(LoginEvent.LoginType.FAIL, userDto.getUsername()));
            throw new QueryException("用户名或密码错误",ResultCode.USERNAME_PASSWORD_ERROR);
        });
        context.publishEvent(new LoginEvent(LoginEvent.LoginType.SUCCESS, userDto.getUsername()));
        return userInfo;
    }

    @Override
    public void updateLogin(String userName, Date loginDate, String ip) {
        sysUserMapper.updateLogin(userName,loginDate,ip);
    }


}
